package com.jk.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jk.controller.BaseController;
import com.jk.pojo.Admin;
import com.jk.service.AdminService;
import com.jk.util.MD5Encrypt;

/**
 * 后台用户模块
 * @author cuiP
 *
 */
@Controller
@RequestMapping("/admin/admin/")
public class AdminController extends BaseController {

	private static final String JSP_PATH = "/admin/";
	
	@Resource
	private AdminService adminService;
	
	@RequiresPermissions(logical=Logical.AND,value={"admin:manage","admin:query"})
	@RequestMapping("list.do")
	public String list(HttpServletRequest request,@RequestParam(required=true,defaultValue="1") String type,ModelMap modelMap){
		List<Admin> List = adminService.queryAll();
		modelMap.put("list", List);
		return JSP_PATH+"list";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("admin:create")
	@RequestMapping("add.do")
	public String add(HttpServletRequest request){
		return JSP_PATH+"add";
	}
	
	/**
	 * 保存
	 * @param request
	 * @return
	 */
	@RequiresPermissions("admin:create")
	@ResponseBody
	@RequestMapping(value="save.do",method=RequestMethod.POST)
	public Map<String, Object> save(HttpServletRequest request,Admin admin){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			Admin searchAdmin = new Admin();
			searchAdmin.setUsername(admin.getUsername());
			
			Admin u = adminService.queryOne(searchAdmin);
			if(u != null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","用户名已存在!");
				return messageMap;
			}
			
			admin.setPassword(MD5Encrypt.MD5Encode(admin.getPassword()));
			admin.setRegisterTime(new Date());
			admin.setRoleId(2);
			admin.setLocked(false);
			adminService.save(admin);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_SAVE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","保存失败!");
		}
		return messageMap;
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete/{id}.do",method=RequestMethod.DELETE)
	public Map<String, Object> delete(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			Admin record = adminService.queryById(id);
			if(record == null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","用户不存在!");
				return messageMap;
			}
			if(Integer.valueOf(1).equals(record.getRoleId())){
				messageMap.put("status", FAILURE);
				messageMap.put("message","该用户不运行操作!");
				return messageMap;
			}
			
			adminService.deleteById(id);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_DELETE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}
	
	/**
	 * 启用||停用
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update_status/{id}.do",method=RequestMethod.PUT)
	public Map<String, Object> update_status(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			Admin record = adminService.queryById(id);
			if(record == null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","用户不存在!");
				return messageMap;
			}
			if(Integer.valueOf(1).equals(record.getRoleId())){
				messageMap.put("status", FAILURE);
				messageMap.put("message","该用户不运行操作!");
				return messageMap;
			}
			
			record.setLocked(!record.getLocked());
			adminService.update(record);
			
			messageMap.put("status", SUCCESS);
			messageMap.put("message","成功!");
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}
}
